<template>
  <div class="account-container">
    <!-- 操作按钮 -->
    <div class="operation-bar">
      <el-button type="primary" @click="handleAddAccount">+ 添加账户</el-button>
      <el-button @click="handleBatchDelete" v-if="selectedAccounts.length > 0">批量删除</el-button>
    </div>

    <!-- 数据表格 -->
    <el-card class="table-card">
      <el-table
        :data="tableData"
        style="width: 100%"
        v-loading="loading"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="序号" width="80" />
        <el-table-column prop="bankName" label="银行/第三方" width="150" />
        <el-table-column prop="accountNo" label="账户/账号" />
        <el-table-column prop=" holderName" label="持卡人姓名" width="120" />
        <el-table-column prop="phone" label="手机号" width="120" />
        <el-table-column label="操作" fixed="right" width="120">
          <template #default="{ row }">
            <el-button link type="primary" @click="handleEdit(row)">编辑</el-button>
            <el-button link type="danger" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加/编辑账户弹窗 -->
    <el-dialog
      :title="dialogTitle"
      v-model="dialogVisible"
      width="500px"
      :close-on-click-modal="false"
    >
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="100px"
        class="account-form"
      >
        <el-form-item label="账户类型" prop="type">
          <el-select v-model="formData.type" placeholder="请选择账户类型" style="width: 100%">
            <el-option label="微信账户" value="wechat" />
            <el-option label="支付宝账户" value="alipay" />
            <el-option label="银行卡" value="bank" />
          </el-select>
        </el-form-item>
        <el-form-item :label="formData.type === 'bank' ? '开户银行' : '账户名称'" prop="bankName">
          <el-input v-model="formData.bankName" placeholder="请输入账户名称" />
        </el-form-item>
        <el-form-item :label="formData.type === 'bank' ? '卡号' : '账号'" prop="accountNo">
          <el-input v-model="formData.accountNo" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item label="持卡人姓名" prop="holderName">
          <el-input v-model="formData.holderName" placeholder="请输入持卡人姓名" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="formData.phone" placeholder="请输入手机号" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, computed } from 'vue'
import { type FormInstance, type FormRules, ElMessage, ElMessageBox } from 'element-plus'

// 表格数据
const tableData = ref([
  {
    id: 1,
    bankName: '微信账户',
    accountNo: '621844208884896143',
    holderName: '何天民',
    phone: '15024058106'
  },
  {
    id: 2,
    bankName: '支付宝账户',
    accountNo: '621844208884896143',
    holderName: '何天民',
    phone: '15024058106'
  },
  {
    id: 3,
    bankName: '交通银行',
    accountNo: '621844208884896143',
    holderName: '何天民',
    phone: '15024058106'
  },
  {
    id: 4,
    bankName: '工商银行',
    accountNo: '621844208884896143',
    holderName: '何天民',
    phone: '15024058106'
  },
  {
    id: 5,
    bankName: '邮政银行',
    accountNo: '621844208884896143',
    holderName: '何天民',
    phone: '15024058106'
  },
  {
    id: 6,
    bankName: '建设银行',
    accountNo: '621844208884896143',
    holderName: '何天民',
    phone: '15024058106'
  },
  {
    id: 7,
    bankName: '中国银行',
    accountNo: '621844208884896143',
    holderName: '何天民',
    phone: '15024058106'
  }
])

const loading = ref(false)
const selectedAccounts = ref<any[]>([])

// 表单相关
const dialogVisible = ref(false)
const dialogTitle = computed(() => (formData.id ? '编辑账户' : '添加账户'))
const formRef = ref<FormInstance>()
const formData = reactive({
  id: '',
  type: '',
  bankName: '',
  accountNo: '',
  holderName: '',
  phone: ''
})

// 表单验证规则
const formRules: FormRules = {
  type: [{ required: true, message: '请选择账户类型', trigger: 'change' }],
  bankName: [{ required: true, message: '请输入账户名称', trigger: 'blur' }],
  accountNo: [{ required: true, message: '请输入账号', trigger: 'blur' }],
  holderName: [{ required: true, message: '请输入持卡人姓名', trigger: 'blur' }],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ]
}

// 处理表格选择
const handleSelectionChange = (selection: any[]) => {
  selectedAccounts.value = selection
}

// 处理添加账户
const handleAddAccount = () => {
  Object.keys(formData).forEach((key) => {
    formData[key as keyof typeof formData] = ''
  })
  dialogVisible.value = true
}

// 处理编辑账户
const handleEdit = (row: any) => {
  Object.assign(formData, row)
  dialogVisible.value = true
}

// 处理删除账户
const handleDelete = async (_row: any) => {
  try {
    await ElMessageBox.confirm('确定要删除该账户吗？', '提示', {
      type: 'warning'
    })
    // TODO: 调用删除API
    ElMessage.success('删除成功')
  } catch {}
}

// 处理批量删除
const handleBatchDelete = async () => {
  try {
    await ElMessageBox.confirm(
      `确定要删除选中的 ${selectedAccounts.value.length} 个账户吗？`,
      '提示',
      { type: 'warning' }
    )
    // TODO: 调用批量删除API
    ElMessage.success('删除成功')
  } catch {}
}

// 处理表单提交
const handleSubmit = async () => {
  if (!formRef.value) return
  await formRef.value.validate(async (valid) => {
    if (valid) {
      // TODO: 调用保存API
      ElMessage.success('保存成功')
      dialogVisible.value = false
    }
  })
}
</script>

<style lang="scss" scoped>
.account-container {
  padding: 20px;

  .operation-bar {
    margin-bottom: 20px;
    display: flex;
    gap: 10px;
  }

  .account-form {
    padding: 20px 0;
  }
}
</style>
